﻿
package system.base;

import framework.core.*;
import system.SelectManager;
import system.data.*;

/*
	Author    :justin
	CreateTime:2008-01-03 16:27:25
*/
public class User extends UserBase 
{
    
//----system generated start----// 	
public User() 
{
	
}

private static void delete(DBConnection con,String id) throws Exception
{
	Conditions conditions = new Conditions();

	conditions.add(ID,id);
	
	delete(con,TABLE_NAME,conditions);
}
public static User fetch()
{
	return new User();
}
public static User copy(User srcObj)
{
	User obj = fetch();
	obj.copyBase(srcObj);
	return obj;
}
public static User fetch(DBConnection con,String id) throws Exception
{
	Conditions conditions = new Conditions();

	conditions.add(ID,id);
	
	//conditions.add(WAREHOUSE_CODE,)
	
	
	return fetch(con,conditions);
}
public static User fetch(DBConnection con,Conditions conditions) throws Exception
{
	DBResult result = null;
	try
	{
		result = DBEngine.doSelect(con,"select * from " + TABLE_NAME,conditions);
		java.sql.ResultSet rs = result.getRs();
		User obj = null;
		if(rs.next())
		{
			obj = fetch(rs);
		}
		return obj;
	}
	finally
	{
		if(result!=null) result.clear() ;
	}
}
public static User fetch(java.sql.ResultSet rs) throws Exception
{
	User obj = new User();
	obj.initObject(rs);
	return obj;
}
protected void getUnique(DBConnection con) throws Exception
{
		setId(DBEngine.getId(con));
}
//----system generated end----//



//----add by justin start----//
  public String viewStatus(DBConnection con) throws Exception
  {
	return SelectManager.viewValidOrNo(getStatus()+"");	
  } 
  
  private void checkUnique(DBConnection con) throws Exception 
  {
	  Conditions cs = new Conditions();
	  cs.add(CODE, getCode());
	  cs.add(ID, Conditions.NOT_EQUAL, getId());
	  if(DBEngine.doCount(con, "select count(id) from "+TABLE_NAME, cs) > 0)
		  throw new BizException("已存在的用户名，请重新填写。");
  }
  public void beforeUpdate(DBConnection con) throws Exception
  {
	  checkUnique(con);
  }
  public void beforeInsert(DBConnection con) throws Exception
  {
	  checkUnique(con);
  } 
  
  //根据用户名和密码得到用户
  public static User fetch(
	  DBConnection con,
	  String userCode,
	  String password)
	  throws Exception
  {
	  Conditions cs = new Conditions();
	  cs.add(CODE, userCode);
	  cs.add(PASSWORD, password);
	  cs.add(STATUS,ConstDefine.STR_YES);
	  return fetch(con, cs);
  }
  
  //根据用户名
  public static User fetchByName(String userCode)
	  throws Exception
  {
	  Conditions cs = new Conditions();
	  cs.add(CODE, userCode);
	  cs.add(STATUS,ConstDefine.STR_YES);
	  return fetch(DBConnection.open(), cs);
  }
   

//----add by justin end----//
  
  // add by Jackey start
  public String viewWarehouseCode(DBConnection con) throws Exception
  {
      String mReturn = "";
      Warehouse w = Warehouse.fetchByCode(con, getWarehouseCode());
      mReturn = w.viewName(con);
      return mReturn;
  }
  // add by Jackey end
  
    //----add by beny start----//
    //重写仓库名称
    public String viewWarehouseName(DBConnection con) throws Exception
    {
        String mReturn = "";
        Warehouse w = Warehouse.fetchByCode(con, getWarehouseCode());
        if (w != null)
        {
            mReturn = w.getName();
        }
        return mReturn;
    }
    //----add by benny end----//
}

